//*****************************************列表框*****************************************
/*
data = [
	{code:'',name:'文本'},
];
@param id
@param data
@param style
@param isFilter			是否有查询
@param isSingleModel	单选框
@param isShowCheck		复选框
@param isShowMove		上下移动按钮
@param isShowDelete		删除按钮
@param isShowValue		显示一行中间部分，可以是对象或者字符串
@param callback
*/
function RxListBox() {
}
RxListBox.init = function (id, data, style, config, callback) {
	var control = document.getElementById(id);
	if (control == undefined || control == null || control == "") {
		control = document.createElement("div");
	}

	if (id) {
		control.id = id;
	}
	control.rxtype = "rxlistbox";
	control.className = "rxlistbox_panel";
	control.filter = "";
	control.data = data;
	control.items = [];
	control.ce = RxCommon.ce;
	control.config = config;
	control.callback = callback;
	
	if (style) {
		RxCommon.setStyle(control, style);
	}

	control.dispose = function(isAll) {
		var areaList = $(control).find(".rxdroplist");
		for(var i = 0; i < areaList.length; i++){
			areaList[i].dispose(true);
		}
		while (this.hasChildNodes()) {
			this.removeChild(this.lastChild);
		}
		if (isAll == true) {
			var parentNode = this.parentNode;
			if (parentNode) {
				parentNode.removeChild(this);
			}
		}
	}

	if (control.config.isFilter == true) {
		var filterInput = control.ce(control,"","input","rxlistbox_filter");
		filterInput.setAttribute('type', 'text');
		filterInput.style.width = parseInt(control.style.width) - 2 + "px";
		filterInput.onkeyup = function() {
			this._owner.filter = this.value;
			this._owner.refresh();
		};
		control.ctrlFilter = filterInput;
	}
	
	var panel1 = control.ce(control,"","div","rxlistbox");
	panel1.style.top = "0px";
	if (control.config.isFilter == true) {
		panel1.style.top = "25px";
	}
	
	var panel = control.ce(panel1,"","div","");
	control.ctrlPanel = panel;



	control.refresh = function() {
		this.setFilter(this.filter);
	};

	control.setFilter = function(filter) {
		while (this.ctrlPanel.hasChildNodes()) {
			this.ctrlPanel.removeChild(this.ctrlPanel.lastChild);
		}
		this.filter = filter;
		if (this.data == undefined || this.data == null) {
			return;
		}

		var topPos = 0;
		for (var i = 0; i < this.data.length; i++) {
			if (filter && this.data[i].name.indexOf(filter) < 0) {
				//this.data[i].selected = false;
				//this.className = "rxlistbox_item";
				continue;
			}
			var item = control.ce(this.ctrlPanel,"","div","rxlistbox_item");
			
			var checkbox = null;
			if (this.config.isShowCheck == true) {
				var className = "rxlist_checkbox";	
				if (this.config.isSingleModel == true) {
					className = "rxlist_radio";
				}
				
				checkbox = control.ce(item,"","div",className);
				item.checkbox = checkbox;
				checkbox.setSelected = function(checked) {
					if (checked == true) {
						this.className = className + "_on";
					} else {
						this.className = className + "_off";
					}
				};
				checkbox.setSelected(false);

				var itemText = control.ce(checkbox,"","div","rxlist_checkbox_text");
				
				var text = control.ce(itemText,"","div","rxlistbox_item_text");
				text.innerHTML = this.data[i].name;
				if (this.data[i].color) {
					text.style.color = this.data[i].color;
				}
				if(this.config.isShowValue == true){
					text.style.width = "20%";
					var value = control.ce(itemText,"","div","rxlistbox_item_value");
					if(this.config.isShowMove || this.config.isShowDelete){
						if(this.config.isShowMove && this.config.isShowDelete){
							value.style.right = "105px";
						}else if(this.config.isShowMove){
							value.style.right = "85px";
						}else{
							value.style.right = "25px";
						}
					}
					if (control.config.isShowFloatDelete == true) {
						value.style.right = "5px";
					}
					if(RxCommon.isString(this.data[i].value)){
						value.innerHTML = this.data[i].value;
					}else{
						this.data[i].value.style.position = "absolute";
						this.data[i].value.style.top = "2px";
						value.appendChild(this.data[i].value);
					}
				}
			}else {
				var textDiv = control.ce(item,"","div","rxlistbox_item_textdiv");
				
				var text = control.ce(textDiv,"","div","rxlistbox_item_text");
				text.innerHTML = this.data[i].name;
				
				if(this.config.isShowValue == true){
					text.style.width = "100px";
					var value = control.ce(textDiv,"","div","rxlistbox_item_value");
					if(this.config.isShowMove || this.config.isShowDelete){
						if(this.config.isShowMove && this.config.isShowDelete){
							value.style.right = "105px";
						}else if(this.config.isShowMove){
							value.style.right = "85px";
						}else{
							value.style.right = "25px";
						}
					}
					if (control.config.isShowFloatDelete == true) {
						value.style.right = "5px";
					}
					if(RxCommon.isString(this.data[i].value)){
						value.innerHTML = this.data[i].value;
					}else{
						this.data[i].value.style.position = "absolute";
						this.data[i].value.style.top = "2px";
						value.appendChild(this.data[i].value);
					}
				}
			}
			
			if (control.config.isShowFloatDelete == true) {
				if(this.data[i].isShowDelete == undefined || (this.data[i].isShowDelete != undefined && this.data[i].isShowDelete)){
					var floatDelete = control.ce(item,"","div","rxmove_arrow_float_delete");
					floatDelete.item = item;
					floatDelete.onclick = function(){
						control.removeItem(this.item.data);
						if(this.self.callback){
							this.self.callback({name:"onFloatRemove", data:this.item.data});
						}
					}
					floatDelete.self = this;
					item.floatDelete = floatDelete;
				}
			}
			
			if (this.config.isShowMove == true || this.config.isShowDelete == true) {
				var movePanel = control.ce(item,"","div","rxmove_arrow");
				if (this.config.isShowMove == true) {
					var moveTop = control.ce(movePanel,"","div","rxmove_arrow_top");
					var moveUp = control.ce(movePanel,"","div","rxmove_arrow_up");
					var moveDown = control.ce(movePanel,"","div","rxmove_arrow_down");
					var moveBottom = control.ce(movePanel,"","div","rxmove_arrow_bottom");
					if(this.config.isShowDelete == false){
						moveTop.style.left = "20px";
						moveUp.style.left = "40px";
						moveDown.style.left = "60px";
						moveBottom.style.left = "80px";
					}
					moveTop.item = item;
					moveUp.item = item;
					moveDown.item = item;
					moveBottom.item = item;
					
					moveTop.data = i;
					moveUp.data = i;
					moveDown.data = i;
					moveBottom.data = i;

					moveTop.onclick = function(e) {
						this._self.revertItem(this.data, 'top');
						e = e || window.event;
						e.stopPropagation();
					};
					moveUp.onclick = function(e) {
						this._self.revertItem(this.data, 'up');
						e = e || window.event;
						e.stopPropagation();
					};
					moveDown.onclick = function(e) {
						this._self.revertItem(this.data, 'down');
						e = e || window.event;
						e.stopPropagation();
					};
					moveBottom.onclick = function(e) {
						this._self.revertItem(this.data, 'bottom');
						e = e || window.event;
						e.stopPropagation();
					};
				}
				if(this.config.isShowDelete == true){
					var moveDelete = control.ce(movePanel,"","div","rxmove_arrow_delete");
				}
				item.movePanel = movePanel;
			}
			item.style.top = topPos + "px";
			item.listIndex = i;
			item.data = this.data[i];
			item.sortno = i;
			item.onmousedown = this.textmousedown;
			item.onmouseup = this.textmouseup;
			item.onclick = function(e) {
				e = e || window.event;
				var target = e.srcElement || e.currentTarget || e.target;
				if(target.className == "rxmove_arrow_float_delete"){
					return;
				}
				if (this._self.config.isSingleModel == true) {
					this._self.data[this.listIndex].selected = true;
					if (this.checkbox) {
						this.checkbox.setSelected(true);
					}
					if (this._self.config.isShowCheck == true) {
						this.className = "rxlistbox_item";
					} else {
						this.className = "rxlistbox_item_selected";
					}
					for (var j = 0; j < this._self.items.length; j++) {
						if (this != this._self.items[j] && this._self.data[this._self.items[j].listIndex].selected == true) {
							if (this._self.items[j].checkbox) {
								this._self.items[j].checkbox.setSelected(false);
							}
							this._self.items[j].className = "rxlistbox_item";
							this._self.data[this._self.items[j].listIndex].selected = false;
							break;
						}
					}
				} else {
					if (this._self.data[this.listIndex].selected == true) {
						this._self.data[this.listIndex].selected = false;
						this.className = "rxlistbox_item";
					} else {
						this._self.data[this.listIndex].selected = true;
						if (this._self.config.isShowCheck == true) {
							this.className = "rxlistbox_item";
						} else {
							this.className = "rxlistbox_item_selected";
						}
					}
					if (this.checkbox) {
						this.checkbox.setSelected(this._self.data[this.listIndex].selected);
					}
				}
				if (this._self.callback) {
					this._self.callback(this._self.data[this.listIndex]);
				}
			}
			item.onmouseover = function() {
				if (this.movePanel) {
					this.movePanel.style.display = "block";
				}
				if (this.floatDelete) {
					this.floatDelete.style.display = "block";
				}
			};
			item.onmouseout = function() {
				if (this.movePanel) {
					this.movePanel.style.display = "none";
				}
				if (this.floatDelete) {
					this.floatDelete.style.display = "none";
				}
			};
			if (this.config.isShowDelete == true) {
				moveDelete.data = this.data[i];
				moveDelete.onclick = function(e) {
					this._self.removeItem(this.data);
					this._self.items = [];
					e = e || window.event;
					e.stopPropagation();
					if (this._self.callback) {
						this._self.callback(this.data);
					}
				};
			}

			if (this.data[i].selected == true) {
				if (this.config.isShowCheck == true) {
					item.className = "rxlistbox_item";
				} else{
					item.className = "rxlistbox_item_selected";
				}
				if (item.checkbox) {
					item.checkbox.setSelected(true);
				}
			}

			this.items[i] = item;
			topPos = topPos + 29;
		}

	};

	control.revertItem = function(srcItem, direction) {
			if (direction == 'top' && srcItem > 0) {
				var pos1 = this.data[0];
				var pos2 = this.data[srcItem];
				this.data[0] = pos2;
				this.data[srcItem] = pos1;
				this.refresh();
			}
			if (direction == 'up' && srcItem > 0) {
				var pos1 = this.data[srcItem - 1];
				var pos2 = this.data[srcItem];
				this.data[srcItem - 1] = pos2;
				this.data[srcItem] = pos1;
				this.refresh();
			}
			if (direction == 'down' && srcItem < this.items.length - 1) {
				var pos1 = this.data[srcItem + 1];
				var pos2 = this.data[srcItem];
				this.data[srcItem + 1] = pos2;
				this.data[srcItem] = pos1;
				this.refresh();
			}
			if (direction == 'bottom' && srcItem < this.items.length - 1) {
				var pos1 = this.data[this.data.length - 1];
				var pos2 = this.data[srcItem];
				this.data[this.data.length - 1] = pos2;
				this.data[srcItem] = pos1;
				this.refresh();
			}
	};

	control.removeItem = function(data) {
		if (data == undefined || data == null || data.length == 0) {
			return;
		}
		//for (var i = 0; i < data.length; i++) {
			for (var j = 0; j < this.data.length; j++) {
				if (this.data[j].name == data.name) {
					this.data.splice(j,1);
					break;
				}
			}
		//}
		this.setFilter(this.filter);
	};
	
	control.removeItemByCode = function(data) {
		if (data == undefined || data == null || data.length == 0) {
			return;
		}
		for (var j = 0; j < this.data.length; j++) {
			if (this.data[j].code == data.code) {
				this.data.splice(j,1);
				break;
			}
		}
		this.setFilter(this.filter);
	};

	control.addItem = function(data) {
		if (data == undefined || data == null || data.length == 0) {
			return;
		}
		for (var i = 0; i < data.length; i++) {
			if (this.isExists(data[i]) == true) {
				continue;
			}
			this.data[this.data.length] = data[i];
		}
		this.setFilter(this.filter);
	};

	control.getSelectedItems = function() {
		var items = [];
		for (var i = 0; i < this.items.length; i++) {
			for (var j = 0; j < this.items.length; j++) {
				if (this.data[this.items[j].listIndex].selected == true && i == this.items[j].sortno) {
					var ob = this.data[this.items[j].listIndex];
					ob.ids = this.items[j].listIndex;
					items[items.length] = ob;
					break;
				}
			}
		}
		return items;
	};
	
	control.createDrag = function() {
        this.dragNode = null;
        this.floatingContainer = document.createElement('UL');
        this.floatingContainer.style.position = 'absolute';
        this.floatingContainer.style.zIndex = "10000";
        this.floatingContainer.style.display = 'none';
        this.floatingContainer.id = '_rxFloatingContainer_' + this.id;
        this.dragDropTimer = -1;
        document.body.appendChild(this.floatingContainer);

        this.initDrag = function(e) {
            e = e || window.event;
            if (e.button != 0) {
            	return;
            }
            var target = e.srcElement || e.currentTarget || e.target;
            var self = target._self;
            window.isRxDragging = true;

            document.documentElement.onselectstart = function(){return false;};
            document.documentElement.ondragstart = function(){return false;};
            document.documentElement.onmousemove = function(event) {
                if(self.dragDropTimer < 10) {
                    return;
                }
                event = event || window.event;
                var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
                var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
                var dragDrop_x = event.clientX/1 + 5 + scrollLeft;
                var dragDrop_y = event.clientY/1 + 5 + scrollTop;
                self.floatingContainer.style.left = dragDrop_x + 'px';
                self.floatingContainer.style.top = dragDrop_y + 'px';
                return false;
            };
            document.documentElement.onmouseup = function(event) {
                event = event || window.event;
                var desTarget = event.srcElement || event.currentTarget || event.target;

                if (self.dragDropTimer < 10) {
                    self.dragDropTimer = -1;
                    return;
                }

                var rxevent = {};
                rxevent.e = event;
                rxevent.name = "ondrag";
                rxevent.self = self._self;
                rxevent.target = desTarget;
                rxevent.source = self.dragNode;
                if(self.callback){
                	self.callback(rxevent);
                }
                

                self.floatingContainer.style.display = 'none';
                while (self.floatingContainer.hasChildNodes()) {
                    self.floatingContainer.removeChild(self.floatingContainer.lastChild);
                }
                self.dragDropTimer = -1;
                window.isRxDragging = false;

                document.documentElement.onselectstart = function(){return true};
                document.documentElement.ondragstart = function(){return true};
                document.documentElement.onmousemove = function(){return true};
                document.documentElement.onmouseup = function(){return true};
            };

            self.dragNode = self.ceDragNode(this.nodeli);
            self.dragDropTimer = 0;
            self.timerDrag();
            return false;
        };
        this.timerDrag = function() {
            var self = this;
            if(self.dragDropTimer >= 0 && self.dragDropTimer < 10) {
                self.dragDropTimer = self.dragDropTimer + 1;
                setTimeout(function(){
                    self.timerDrag();
                }, 10);
                return;
            }
            if(this.dragDropTimer == 10) {
                self.floatingContainer.style.display = 'block';
                self.floatingContainer.appendChild(self.dragNode);
            }
        };
    };
    
	control.isExists = function(item) {
		var isExists = false;
		if (!item) {
			return isExists;
		}
		for (var i = 0; i < this.data.length; i++) {
			if (this.data[i].code == item.code) {
				isExists = true;
				break;
			}
		}
		return isExists;
	};

	control.refresh();

	return control;
};
//*****************************************列表框*****************************************